﻿using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Office.Interop.Excel;


namespace NFLManager
{
    public static class ExcelManager
    {
        private static Application xlApp = null;
        private static Workbook xlWorkbook = null;
        private static Worksheet xlWorksheet = null;

        //devo definire le posizioni di partenza, nel report xls, delle varie tabelle 
        private static Dictionary<string, int[]> startPositions; 

        public static void GetReport()
        {
            xlApp.DisplayFullScreen = true;
        }

        public static void createDoc(bool visible = false)
        {
            try
            {
                xlApp = new Application();
                xlApp.Visible = visible;
                

                
                xlWorkbook = xlApp.Workbooks.Add(Missing.Value);

                createSheets();
                
            }
            catch (Exception)
            {
                
            }
        }

        private static void createSheets()
        {
            xlWorksheet = (Worksheet)xlWorkbook.Sheets[1];
            xlWorksheet.Select(Type.Missing);
            xlWorksheet = (Worksheet)xlWorkbook.ActiveSheet;
            xlWorksheet.Name = "GEN";
            xlWorksheet = (Worksheet)xlWorkbook.Sheets[2];
            xlWorksheet.Select(Type.Missing);
            xlWorksheet = (Worksheet)xlWorkbook.ActiveSheet;
            xlWorksheet.Name = "OFF";
            xlWorksheet = (Worksheet)xlWorkbook.Sheets[3];
            xlWorksheet.Select(Type.Missing);
            xlWorksheet = (Worksheet)xlWorkbook.ActiveSheet;
            xlWorksheet.Name = "DEF";
        }

        public static void openSheet(string tab)
        {
            xlWorksheet = (Worksheet) xlWorkbook.Sheets[tab];
            xlWorksheet.Select(Type.Missing);
            xlWorksheet = (Worksheet) xlWorkbook.ActiveSheet;
        }

        //public static void writeReportXLS(Team team)
        //{
        //    int x = 2;
        //    int y = 2;
        //    foreach (var table in team.Stats.Tables)
        //    {
        //        createDoc();
        //        openSheet("GEN");
        //        var startCell = xlWorksheet.Cells[y, x];
        //        var endCell = xlWorksheet.Cells[y + table.rows.team.Stats[entry].GetLength(0) - 1, ColumnIndex: x + team.Stats[entry].GetLength(1) - 1];
        //        try
        //        {
        //            // access range by Property and cells indicating start and end           
        //            var writeRange = xlWorksheet.Range[startCell, endCell];
        //            writeRange.Value = team.Stats[entry];
        //            writeRange.Columns.AutoFit();
        //            finalModifySaveAndClose(Settings.Default.DATA_PATH + team.Id );
        //        }
        //        catch (COMException ex)
        //        {
        //            Debug.WriteLine(ex.Message);
        //            Debugger.Break();
        //        }
        //    }
            
        //}
        
        public static void finalModifySaveAndClose(string docFullPath)
        {
            string relPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            xlWorkbook.SaveAs(relPath + docFullPath);
            xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
            xlApp.Quit();
        }
    }
}
